package com.nightpiter;

import static com.nightpiter.controller.log.Logger.logProfilerBegin;
import static com.nightpiter.controller.log.Logger.logProfilerEnd;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

import com.nightpiter.controller.MetroController;
import com.nightpiter.model.MetroModel;
import com.nightpiter.view.MetroView;

public class NightPiter extends Activity {

	/**
	 * MVC Model
	 */
	private MetroModel model;

	/**
	 * MVC View
	 */
	private MetroView view;

	/**
	 * MVC Controller
	 */
	private MetroController controller;

	/** Called when the activity is first created. */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		com.nightpiter.controller.log.Logger.openLog(this);

		initMVC();

		com.nightpiter.controller.log.Logger.saveLog();

	}

	/**
	 * creates and initializes MVC components
	 */
	private void initMVC() {

		logProfilerBegin("Instantiating MVC components");
		this.model = new MetroModel(this);
		this.view = new MetroView(this);
		this.controller = new MetroController(this);
		logProfilerEnd("All components have been instantiated");

		logProfilerBegin("Calling MVC components init() methods");
		this.model.init();
		this.view.init();
		this.controller.init();
		logProfilerEnd("MVC init complete");

	}

	private static final int OPEN_FILE_MENU_ITEM = Menu.FIRST;

	private static final int ZOOM_MENU_ITEM = Menu.FIRST + 1;

	private static final int ABOUT_MENU_ITEM = Menu.FIRST + 2;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, OPEN_FILE_MENU_ITEM, "Open new map", R.drawable.openmap);
		menu.add(0, ABOUT_MENU_ITEM, "About", R.drawable.matmex);
		menu.add(0, ZOOM_MENU_ITEM, "Zoom", R.drawable.zoom);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item) {
		switch (item.getId()) {
		case OPEN_FILE_MENU_ITEM:
			this.view.onOpenFileMenuItemSelected();
			return true;
		case ABOUT_MENU_ITEM:
			this.view.onAboutMenuItemSelected();
			return true;
		case ZOOM_MENU_ITEM:
			this.view.onZoomMenuItemSelected();
			return true;
		}
		return false;
	}

	/**
	 * @return the model
	 */
	public MetroModel getModel() {
		return model;
	}

	/**
	 * @return the view
	 */
	public MetroView getView() {
		return view;
	}

	/**
	 * @return the controller
	 */
	public MetroController getController() {
		return controller;
	}
}
